<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Python 的文件类型 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Python 的文件类型</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Python/">Python</a>
			</span>
		
	</div>

	

	
		<h1 id="Python-的文件类型"><a href="#Python-的文件类型" class="headerlink" title="Python 的文件类型"></a>Python 的文件类型</h1><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的使用我们可以用解释器交互方式 python 和 ipython，也可以建立一个程序文件。</p>
<h2 id="1-源代码"><a href="#1-源代码" class="headerlink" title="1.源代码"></a>1.源代码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 源代码文件以 “py” 为扩展名，由 Python 程序解释，不需要编译。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># vim 1.py</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;写入内容</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"><span class="keyword">print</span> <span class="string">'hello world'</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：第一行必须写  <code>#!/usr/bin/python</code> ，因为第一行表示用什么解释器来解释文件，如果不写就是以 bash 解释。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 1.py</span></div><div class="line">hello world</div><div class="line">[root@192 day01]<span class="comment"># chmod u+x 1.py </span></div><div class="line">[root@192 day01]<span class="comment"># ./1.py</span></div><div class="line">hello world</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 的执行和 bash 很相似。</p>
<h2 id="2-字节代码"><a href="#2-字节代码" class="headerlink" title="2.字节代码"></a>2.字节代码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Python 源码文件经编译后生成的扩展名为 “pyc” 的文件</p>
<h3 id="编译方法："><a href="#编译方法：" class="headerlink" title="编译方法："></a>编译方法：</h3><blockquote>
<p>import py_compile<br>py_compile.compile(‘hello.py’)</p>
</blockquote>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># vim 2.py</span></div></pre></td></tr></table></figure>
<p>写入内容</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#!/usr/bin/python</span></div><div class="line"> </div><div class="line"><span class="keyword">import</span> py_compile</div><div class="line"> </div><div class="line">py_compile.compile(<span class="string">'1.py'</span>)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 2.py</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发现执行以后什么输出都没有</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/01.png?raw=true" alt="iamges"><figcaption class="figure__caption">iamges</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ls 查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/02.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发现多了一个 1.pyc ，这个 1.pyc 就是对 1.py 进行编译<br>  后产生。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看文件类型</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># file 1.pyc</span></div><div class="line">1.pyc: python 2.6 byte-compiled</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;显示被字节编译过<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看 1.pyc 文件，发现是二进制文件</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/03.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 1.pyc</span></div><div class="line">hello world</div></pre></td></tr></table></figure>
<h2 id="3-优化代码"><a href="#3-优化代码" class="headerlink" title="3.优化代码"></a>3.优化代码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;经过优化的源码文件，扩展名为 “pyo”</p>
<h3 id="编译方法：-1"><a href="#编译方法：-1" class="headerlink" title="编译方法："></a>编译方法：</h3><blockquote>
<p>python -o -m py_compile hello.py<br>-O 表示优化<br>-m 表示模块</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python -O -m py_compile 1.py</span></div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/python%E7%9A%84%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B/04.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生成一个 1.pyo 的文件。1.pyo 也是经过编译的，只是多了有一个优化。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 day01]<span class="comment"># python 1.pyo</span></div><div class="line">hello world</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不想让别人查看源码文件就可以把源码文件编译成 pyc 或 pyo 的文件，不过建议使用源码文件。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Python/1. Python 基础篇/1. Python 基础语法/1.3 Python 的文件类型/4. Python 的文件类型/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
